package com.todofun.common.app;

import java.util.Properties;

import org.apache.log4j.Logger;

public class ConfigUtil {
	private static String CONFIG_FILENAME = "server.properties";
	
	private static Properties properties = null;

	private static final Logger logger = Logger.getLogger(ConfigUtil.class);

	public synchronized static void init() {
		if (isInit())
			return;
		loadProperties();
	}
	public static String getProperty(String key) {
		init();
		return properties.getProperty(key);
	}
	public static synchronized void setProperty(String key, String value) {
		init();
		properties.setProperty(key, value);
	}

	public static boolean isInit() {
		return properties != null;
	}
	private static void loadProperties() {
		try {
			Properties cfgProp = new Properties();
			cfgProp.load(ConfigUtil.class.getClassLoader().getResourceAsStream(CONFIG_FILENAME));
			properties = cfgProp;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("配置文件丢失,系统可能无法运行: " + e.toString());
		}
	}	
}
